//
//Copyright GX. Yuan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file net.go
 * @brief Leetcode 剑指Offer 第二版
 * @details 剑指 Offer 43. 1～n 整数中 1 出现的次数
 * @author GX. Yuan  any question please send mail to yuanguanxu@qq.com
 * @date 2021-6-28
 * @version V1.0
 * @attention 硬件平台: windows 10 家庭版
 * SDK版本：Go 1.16.5
 * IDE版本：GoLand 2020.2.3
 */

package main

import (
	"fmt"
	"math"
	"strconv"
)

func countDigitOne(n int) int {
	str := strconv.Itoa(n) // 这里是字符创 12 ,使用string(int)的结果是：string 为'\f' 转变为对应的字符了
	sum := 0
	var high, digit, low int
	for i, _ := range str {
		switch str[i] {
		case '0':
			high, _ = strconv.Atoi(str[:i])
			digit = int(math.Pow10(len(str) - i - 1))
			sum += high * digit
			break
		case '1':
			high, _ = strconv.Atoi(str[:i])
			digit = int(math.Pow10(len(str) - i - 1))
			low, _ = strconv.Atoi(str[i+1:])
			sum += high*digit + low + 1
			break
		default:
			high, _ = strconv.Atoi(str[:i])
			digit = int(math.Pow10(len(str) - i - 1))
			sum += (high + 1) * digit
		}
	}
	return sum
}
func main() {
	n := 12
	fmt.Println(countDigitOne(n))
}

// 根据官方的想法
// 对于这个数，通过 1 这个数字所在的位置。来推断出可能性;
